package com.mars.service;

import com.google.common.base.Preconditions;
import com.mars.beans.PageQuery;
import com.mars.beans.PageResult;
import com.mars.common.RequestHolder;
import com.mars.dao.SysAclMapper;
import com.mars.exception.ParamException;
import com.mars.param.AclParam;
import com.mars.pojo.SysAcl;
import com.mars.util.BeanValidator;
import com.mars.util.IpUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author MARS
 * @date 2018/1/24
 */
@Service
public class SysAclService {

    @Resource
    private SysAclMapper sysAclMapper;
    @Resource
    private SysLogService sysLogService;

    public void save (AclParam param) {
        // 参数校验
        BeanValidator.check(param);
        // 校验是否存在
        if ( checkExist(param.getAclModuleId(),param.getName(),param.getId())) {
            throw new ParamException("当前权限模块下面存在相同名称的权限点");
        }

        // 组装数据
        SysAcl sysAcl = SysAcl.builder().name(param.getName()).aclModuleId(param.getAclModuleId()).url(param.getUrl())
                .type(param.getType()).status(param.getStatus()).seq(param.getSeq()).remark(param.getRemark()).build();

        // 放入编码
        sysAcl.setCode(generateCode());
        sysAcl.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysAcl.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        sysAcl.setOperateTime(new Date());

        sysAclMapper.insertSelective(sysAcl);
        sysLogService.saveAclLog(null,sysAcl);
    }

    public void update (AclParam param) {
        // 参数校验
        BeanValidator.check(param);
        // 校验是否存在
        if ( checkExist(param.getAclModuleId(),param.getName(),param.getId())) {
            throw new ParamException("当前权限模块下面存在相同名称的权限点");
        }

        // 获取之前的值,若为空，则不能进行更新
        SysAcl before = sysAclMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before,"待更新的权限点不存在");

        // 组装数据
        SysAcl after = SysAcl.builder().id(param.getId()).name(param.getName()).aclModuleId(param.getAclModuleId()).url(param.getUrl())
                .type(param.getType()).status(param.getStatus()).seq(param.getSeq()).remark(param.getRemark()).build();
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        after.setOperateTime(new Date());

        sysAclMapper.updateByPrimaryKeySelective(after);
        sysLogService.saveAclLog(before,after);
    }

    // 校验是否重复
    public boolean checkExist(int aclModuleId,String name,Integer id){
        return sysAclMapper.countByNameAndAclModuleId(aclModuleId,name,id) > 0 ;
    }

    // 权限点编码生成规则
    public String generateCode() {
        // 可以利用时间戳生成编码(唯一)
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date()) + "_" + (int)(Math.random() * 100);
    }

    /**
     * 根据权限模块id获取权限点且分页
     * @param aclModuleId
     * @param page
     * @return
     */
    public PageResult<SysAcl> getPageByAclModuleId (int aclModuleId,PageQuery page) {
        // 校验分页参数信息，有误则抛出异常
        BeanValidator.check(page);
        // 获取数量
        int count = sysAclMapper.countByAclModuleId(aclModuleId);
        if (count > 0 ) {
            List<SysAcl> sysAclList = sysAclMapper.getPageByAclModuleId(aclModuleId,page);
            // 强制转化
            return PageResult.<SysAcl>builder().data(sysAclList).total(count).build();
        }
        // 直接返回空信息
        return PageResult.<SysAcl>builder().build();
    }
}
